package com.seeyon.apps.exportExcelCtrl.dao.impl;

import com.seeyon.apps.exportExcelCtrl.dao.ExportExcelCtrlDao;
import com.seeyon.apps.exportExcelCtrl.units.ExportExcelCtrlUtil;
import com.seeyon.ctp.common.exceptions.BusinessException;
import com.seeyon.ctp.util.JDBCAgent;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Title ExportExcelCtrlDaoImpl
 * @description: 底表导出自定义按钮dao层实现类
 * @author wxt.daicheng
 * @date 2024/8/20 
 * @version 1.0
 */
public class ExportExcelCtrlDaoImpl implements ExportExcelCtrlDao {

    @Override
    public List<Map<String, Object>> getLeaderList(Map<String, Object> params) throws BusinessException {
        JDBCAgent jdbcAgent = null;
        try {
            jdbcAgent = new JDBCAgent(false, false);
            StringBuilder sql = new StringBuilder("select id from ");
            sql.append(params.get("tableName"));

            List<Map<String, Object>> datas = jdbcAgent.findNamedSql(sql.toString());
            return datas;
        } catch (Exception e) {
            ExportExcelCtrlUtil.log("执行查询发生异常","领导数据导出", e);
            throw new BusinessException(e);
        } finally {
            if(jdbcAgent != null) {
                jdbcAgent.close();
            }
        }
    }

    @Override
    public List<Map<String, Object>> getExportExcelList(String tableName, String field,List<String> leaderList) throws BusinessException {
        JDBCAgent jdbcAgent = null;
        try {
            jdbcAgent = new JDBCAgent(false, false);
            StringBuilder sql = new StringBuilder("select * from ");
            sql.append(tableName);
            sql.append(" where ");
            Map<String, Object> parmas = new HashMap<>();
            for (int i = 0; i < leaderList.size(); i++) {
                if (i == 0){
                    sql.append(field).append(" like ");
                    sql.append(":userId").append(i);
                    parmas.put("userId" + i, "%" + leaderList.get(i) + "%");
                }else {
                    sql.append(" or ");
                    sql.append(field).append(" like ");
                    sql.append(":userId").append(i);
                    parmas.put("userId" + i, "%" + leaderList.get(i) + "%");
                }
            }
            sql.append(" order by start_date");

            List<Map<String, Object>> datas = jdbcAgent.findNamedSql(sql.toString(), parmas);
            return datas;
        } catch (Exception e) {
            ExportExcelCtrlUtil.log("执行查询发生异常","领导数据导出", e);
            throw new BusinessException(e);
        } finally {
            if(jdbcAgent != null) {
                jdbcAgent.close();
            }
        }
    }
}
